<?php
/**
 * Created by 视频类.
 * User: 尚金殿
 * Date: 2018/8/26
 * Time: 12:49
 */

namespace app\admin\controller;


use think\Db;

class VideoController extends CommonController
{
    protected $mysql = 'island_video';
    protected $pk = 'id';
    protected $name = 'title';
    protected $status = 'status';


    //添加视频
    public function add(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();

            //判定数据
            if(!$data['island_id'] || !is_numeric($data['island_id'])){
                return show(0,'所属岛屿不能为空');
            }
            if(!$data['title']){
                return show(0,'视频标题不能为空');
            }

            //判定有没有ID
            if(!empty($data['id'])){
                $this->save($data);
            }
            //入库
            $videoId = $this->getAdd($data);

            if(!$videoId){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');

        }else{
            //获取岛屿
            $island = Db::name('island')->where('status',1)->select();

            //变量输出
            $this->assign('island',$island);

            //加载模版
            return $this->fetch();
        }
    }

    //视频编辑
    public function edit(){
        $id = $this->request->param('id');

        //获取视频数据
        $video = Db::name($this->mysql)->where($this->pk,$id)->find();
        //获取岛屿
        $island = Db::name('island')->where('status',1)->select();

        //变量输出
        $this->assign(['video'=>$video,'island'=>$island]);

        //加载模版
        return $this->fetch();
    }

    //保存编辑
    public function save($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }

        //获取ID
        $id = $data['id'];
        unset($data['id']);

        $data['update_time'] = time();

        //入库
        $res = Db::name($this->mysql)->where($this->pk,$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
            return show(1,'更新成功');
    }
}